(function () {
    'use strict';

    angular
        .module('SURF')
        .controller('A01D01Controller', A01D01Controller);



    A01D01Controller.$inject = ['$scope', 'A01D01Service','$translate','$http'];

    function A01D01Controller($scope, A01D01Service,$translate,$http) {
        setMenuActivite('a01','a01d01');

        $("#a01").addClass("active");
        $("#a01d01").addClass("active");

        A01D01Service.getAllLine().then(
            function (answer) {
                var response = answer.data;
                if (response.error_flg == 0) {
                    $scope.lines = response.lines;
                    $scope.changeLine = function () {
                        var line_id =$scope.lineInfo;
                        A01D01Service.getVersion(line_id).then(
                            function (answer) {
                                var response = answer.data;
                                if (response.error_flg == 0) {

                                    $scope.versions = null;
                                    $scope.versionInfo = null;
                                    $scope.versions = response.versions;
                                }else{
                                    showCommonDialog(response.msg,2);
                                }
                            }, function (error) {
                                $scope.error = error;
                            })
                    };



                } else {
                    showCommonDialog(response.msg,2);
                }
            }, function (error) {
                $scope.error = error;
            });


        $scope.checked = [];
        $scope.selectAll = function () {
            if($scope.select_all) {
                $scope.checked = [];
                angular.forEach($scope.devices, function (i) {
                    i.checked = true;
                    $scope.checked.push(i.equipment_id);
                })
            }else {
                angular.forEach($scope.devices, function (i) {
                    i.checked = false;
                    $scope.checked = [];
                })
            }
            console.log($scope.checked);
        };

        $scope.selectOne = function (deviceId) {
            angular.forEach($scope.devices , function (i) {
                if((i.equipment_id == deviceId)){
                    if(i.checked) {
                        $scope.checked.push(i.equipment_id);
                    }else {
                        var index = $scope.checked.indexOf(deviceId);
                        $scope.checked.splice(index, 1);
                    }
                }
            });

            if ($scope.devices.length === $scope.checked.length) {
                $scope.select_all = true;
            } else {
                $scope.select_all = false;
            }
            console.log($scope.checked);
        }


        //提交按钮对应事件
        $scope.searchUserInfo = searchUserInfo;

        function searchUserInfo() {

            var agentInfoBean = {};

            if(isNull($scope.lineInfo)){
                showCommonDialog("请选择产线",2);
                return;
            }

            if(isNull($scope.versionInfo)){
                showCommonDialog("请选择版本",2);
                return;
            }



            A01D01Service.getDevices($scope.lineInfo,$scope.versionInfo).then(
                function (answer) {
                    var response = answer.data;
                    if (response.error_flg == 0) {
                        $scope.devices = null;
                        $scope.devices = response.equipments;
                    }else {
                        showCommonDialog(response.msg,2);
                    }
                }, function (error) {
                }
            )
        };


        $scope.saveSettings = saveSettings;

        function saveSettings() {

            var agentInfoBean = {};

            if(isNull($scope.lineInfo)){
                showCommonDialog("请选择产线",2);
                return;
            }

            if(isNull($scope.versionInfo)){
                showCommonDialog("请选择版本",2);
                return;
            }

            if($scope.checked.length == 0){
                showCommonDialog("请选择设备",2);
                return;
            }



            A01D01Service.saveSettings($scope.lineInfo,$scope.versionInfo,$scope.checked).then(
                function (answer) {
                    var response = answer.data;
                    if (response.error_flg == 0) {
                        showCommonDialog(response.msg,1);
                    }else {
                        showCommonDialog(response.msg,2);
                    }
                }, function (error) {
                }
            )
        };



    }

})();


